package com.circle.interests.biz.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.circle.common.core.constant.Constant;
import com.circle.common.core.enums.Switch;
import com.circle.common.core.exception.RRException;

import com.circle.interests.biz.entity.PrizeCategory;
import com.circle.interests.biz.mapper.PrizeCategoryMapper;
import com.circle.interests.biz.request.PrizeCategoryPageRequest;
import com.circle.interests.biz.request.PrizeCategoryRequest;
import com.circle.interests.biz.service.IPrizeCategoryService;
import com.circle.interests.biz.service.IPrizeInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 奖品分类 服务实现类
 * </p>
 *
 * @author generator
 * @since 2021-12-05
 */
@Slf4j
@Service
@Transactional
public class PrizeCategoryServiceImpl extends ServiceImpl<PrizeCategoryMapper, PrizeCategory> implements IPrizeCategoryService {

    @Resource
    private IPrizeInfoService prizeInfoService;

    @Override
    public IPage<PrizeCategory> page(PrizeCategoryPageRequest form) {
        Page<PrizeCategory> page = new Page<>();
        page.setCurrent(Convert.toLong(form.getPageNo(), Constant.PAGE_NO_1));
        page.setSize(Convert.toLong(form.getPageSize(), Constant.PAGE_SIZE_25));
        Wrapper<PrizeCategory> query = Wrappers.lambdaQuery(PrizeCategory.class)
                .like(StrUtil.isNotBlank(form.getName()), PrizeCategory::getName, form.getName())
                .orderByDesc(PrizeCategory::getId);
        return page(page, query);
    }

    @Override
    public PrizeCategory add(PrizeCategoryRequest form) {
        PrizeCategory entity = getByName(form.getName());
        if (entity != null) {
            throw new RRException("该类型已存在");
        }
        entity = new PrizeCategory();
        BeanUtils.copyProperties(form, entity);
        entity.setStatus(Switch.ON.getCode());
        entity.setId(null);
        save(entity);
        return entity;
    }

    @Override
    public void updateById(PrizeCategoryRequest form) {
        PrizeCategory entity = getByName(form.getName());
        if (entity != null && !Objects.equals(entity.getId(), form.getId())) {
            throw new RRException("该类型已存在");
        }
        if (entity == null) {
            entity = getById(form.getId());
        }
        entity.setName(form.getName());
        entity.setRemark(form.getRemark());
        updateById(entity);
    }

    @Override
    public void deleteById(Long id) {
        if (prizeInfoService.hasPrize(id)) {
            throw new RRException("该类型下已有奖品无法删除");
        }
        removeById(id);
    }

    @Override
    public PrizeCategory getByName(String name) {
        return getOne(Wrappers.lambdaQuery(PrizeCategory.class)
                .eq(PrizeCategory::getName, name).last("limit 1"));
    }

}
